package org.example.myspring02.SqlMapper;

import jakarta.websocket.Session;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.example.myspring02.Bean.PersonBean;
import org.example.myspring02.Mapper.PersonMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.InputStream;
import java.sql.ResultSet;
import java.sql.SQLException;

@RestController
public class SQLServer {
    @Autowired
    private SQLController sqloperate;
    private static SqlSessionFactory sqlSessionFactory;

    static {
        InputStream stream;
        try {
            stream = Resources.getResourceAsStream("Mybatis.xml");
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(stream);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("insert")
    public String insert(PersonBean personBean) throws RuntimeException {
        String name = personBean.getName();
        System.out.println("name:" + name);
        int age = personBean.getAge();
        String gender = personBean.getGender();
        String address = personBean.getAddress();
        if (name == null || age == 0 || gender == null || address == null) {
            return "参数不能为空";
        } else if (age < 0) {
            return "年龄不能为负数";
        }
//        try {
//            sqloperate.insert(name, age, gender, address);
//        } catch (SQLException e) {
//            throw new RuntimeException(e);
//        }
//        return "插入成功";
        try (SqlSession sqlSession = sqlSessionFactory.openSession(true)) {
            PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
            int result = personMapper.insertPerson(personBean);
            if (result == 0) {
                return "插入失败";
            } else {
                return "插入成功";
            }
        }
    }

    @RequestMapping("delete")
    public String deleteById(PersonBean personBean) throws RuntimeException {
        if (personBean.getId() == 0) return "参数不能为空";
        if (personBean.getId() < 0) return "id不能为负数";
        try (SqlSession sqlSession = sqlSessionFactory.openSession(true)) {
            PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
            int result = personMapper.deletePerson(personBean.getId());
            if (result == 0) {
                return "删除失败";
            } else {
                return "删除成功";
            }
        }
    }

    @RequestMapping("select")
    public String selectById(PersonBean personBean) throws RuntimeException {
        if (personBean.getId() == 0) return "参数不能为空";
        if (personBean.getId() < 0) return "id不能为负数";
        try (SqlSession sqlSession = sqlSessionFactory.openSession()) {
            PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
            PersonBean result = personMapper.selectPerson(personBean.getId());
         return result.toString();
        }
    }
}
